package com.mujin.keji.collectionsystem.widget;

import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.dialog.BaseNiceDialog;
import com.mujin.keji.collectionsystem.dialog.NiceDialog;
import com.mujin.keji.collectionsystem.dialog.ViewConvertListener;
import com.mujin.keji.collectionsystem.dialog.ViewHolder;
import com.mujin.keji.collectionsystem.utils.DateUtil;
import com.mujin.keji.collectionsystem.widget.loopview.LoopView;
import com.mujin.keji.collectionsystem.widget.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 选择时间对话框
 *
 * @author weishixiong
 * @Time 2018-05-24
 */
public class SelectTimeDailog {
    private List<String> months;
    private List<String> days;
    private List<String> hours;
    private List<String> mines;
    private int selectYear, selectMonth, selectDay, selectHour, selectMin;
    private FragmentActivity context;
    private GetSelectTimeListener listener;
    private int daySize;//指定年月的 天数
    private int currentYear;
    private int lastYear;
    private int nextYear;

    public interface GetSelectTimeListener {
        void onSelectYearMonthDayHourMin(String date);
    }

    /**
     * 初始化天数
     */
    private void initDays() {
        //获取指定年月的 天数
        daySize = DateUtil.getMonthLastDay(selectYear, selectMonth);
        days = new ArrayList<>();
        for (int i = 1; i <= daySize; i++) {
            if (i < 10) {
                days.add("0" + i);
            } else {
                days.add(i + "");
            }
        }
    }

    public SelectTimeDailog(FragmentActivity context, GetSelectTimeListener getSelectTimeListener) {
        this.context = context;
        this.listener = getSelectTimeListener;
        //初始化月 时 分
        initMonthHourMineDatas();

    }

    /**
     * 初始化月  时 分 数据
     */
    private void initMonthHourMineDatas() {
        months = new ArrayList<>();
        hours = new ArrayList<>();
        mines = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            if (i < 10) {
                months.add("0" + i);
            } else {
                months.add(i + "");
            }
        }
        for (int i = 1; i <= 24; i++) {
            if (i < 10) {
                hours.add("0" + i);
            } else {
                hours.add(i + "");
            }
        }

        for (int i = 0; i <= 59; i++) {
            if (i < 10) {
                mines.add("0" + i);
            } else {
                mines.add(i + "");
            }
        }
    }

    private TextView tvOneYear;
    private TextView tvTwoYear;
    private TextView tvThreeYear;
    private TextView tvOk;
    private LoopView lvMonth;
    private LoopView lvDay;
    private LoopView lvHour;
    private LoopView lvMin;
    private TextView tvCancel;
    private RelativeLayout rlCancel;


    private int[] photos = new int[]{R.drawable.shape_input_to_visit, R.drawable.shape_button_select_bg};
    private String[] colors = new String[]{"#999999", "#FFFFFF"};

    /**
     * 年 月 日
     */
    public void showSelectYearMonthDayDialog(final int year, final int month) {
        this.currentYear = year;
        this.nextYear = year + 1;
        this.lastYear = year - 1;
        this.selectYear = year;
        this.selectMonth = month;
        initDays();
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_year_month_day)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        tvOneYear = holder.getView(R.id.tv_one_year);
                        tvTwoYear = holder.getView(R.id.tv_two_year);
                        tvThreeYear = holder.getView(R.id.tv_three_year);
                        tvOk = holder.getView(R.id.tv_ok);
                        lvMonth = holder.getView(R.id.lv_month);
                        lvDay = holder.getView(R.id.lv_day);
                        lvHour = holder.getView(R.id.lv_hour);
                        lvMin = holder.getView(R.id.lv_min);
                        tvCancel = holder.getView(R.id.tv_cancel);
                        rlCancel = holder.getView(R.id.rl_cancel);
                        tvOneYear.setText(lastYear + "");
                        tvTwoYear.setText(currentYear + "");
                        tvThreeYear.setText(nextYear + "");
                        // 设置原始数据
                        lvMonth.setItems(months);
                        lvDay.setItems(days);
                        lvHour.setItems(hours);
                        lvMin.setItems(mines);
                        lvMonth.setCurrentPosition(selectMonth - 1);
                        lvDay.setCurrentPosition(Calendar.getInstance().get(Calendar.DAY_OF_MONTH) - 1);
                        selectDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
                        lvHour.setCurrentPosition(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) - 1);
                        selectHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                        lvMin.setCurrentPosition(Calendar.getInstance().get(Calendar.MINUTE));
                        selectMin = Calendar.getInstance().get(Calendar.MINUTE);
                        initSelectYear(selectYear);
                        lvMonth.setListener(new OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(int index) {
                                selectMonth = Integer.parseInt(months.get(index));
                                initDays();
                                lvDay.setItems(days);

                            }
                        });
                        lvDay.setListener(new OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(int index) {
                                selectDay = Integer.parseInt(days.get(index));


                            }
                        });
                        lvHour.setListener(new OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(int index) {
                                selectHour = Integer.parseInt(hours.get(index));

                            }
                        });
                        lvMin.setListener(new OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(int index) {

                                selectMin = Integer.parseInt(mines.get(index));


                            }
                        });
                        rlCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                dialog.dismiss();
                                String date = selectYear + "" + (selectMonth < 10 ? ("0" + selectMonth) : (selectMonth))
                                        + "" + (selectDay < 10 ? ("0" + selectDay) : (selectDay)) + "  " + (selectHour < 10 ? ("0" + selectHour) : (selectHour)) + ":"
                                        + (selectMin < 10 ? ("0" + selectMin) : (selectMin));
                                listener.onSelectYearMonthDayHourMin(date);
                            }
                        });
                        tvCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        tvOneYear.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                selectYear = lastYear;
                                initDays();
                                lvDay.setItems(days);
                                initSelectYear(selectYear);
                            }
                        });
                        tvTwoYear.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                selectYear = currentYear;
                                initDays();
                                lvDay.setItems(days);
                                initSelectYear(selectYear);
                            }
                        });
                        tvThreeYear.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                selectYear = nextYear;
                                initDays();
                                lvDay.setItems(days);
                                initSelectYear(selectYear);
                            }
                        });


                    }
                })
                .setShowBottom(true)
                .show(context.getSupportFragmentManager());
    }

    /**
     * 显示要选中的年
     *
     * @param selectYear
     */
    private void initSelectYear(int selectYear) {
        if (selectYear == lastYear) {
            tvOneYear.setTextColor(Color.parseColor(colors[1]));
            tvOneYear.setBackgroundResource(photos[1]);

            tvTwoYear.setTextColor(Color.parseColor(colors[0]));
            tvTwoYear.setBackgroundResource(photos[0]);
            tvThreeYear.setTextColor(Color.parseColor(colors[0]));
            tvThreeYear.setBackgroundResource(photos[0]);

        } else if (selectYear == currentYear) {
            tvTwoYear.setTextColor(Color.parseColor(colors[1]));
            tvTwoYear.setBackgroundResource(photos[1]);

            tvOneYear.setTextColor(Color.parseColor(colors[0]));
            tvOneYear.setBackgroundResource(photos[0]);

            tvThreeYear.setTextColor(Color.parseColor(colors[0]));
            tvThreeYear.setBackgroundResource(photos[0]);

        } else if (selectYear == nextYear) {
            tvThreeYear.setTextColor(Color.parseColor(colors[1]));
            tvThreeYear.setBackgroundResource(photos[1]);

            tvOneYear.setTextColor(Color.parseColor(colors[0]));
            tvOneYear.setBackgroundResource(photos[0]);

            tvTwoYear.setTextColor(Color.parseColor(colors[0]));
            tvTwoYear.setBackgroundResource(photos[0]);

        }
    }


}
